
/*
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

/*
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.

    To request the provided software under a different license you can contact us at
    support@xoronos.com
*/

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "examples.h"
#include "xrn_crypto.h"
#include "xrn_core.h"
#include "lorem_ipsum.h"

    /////////////////////////////////////////////////////
    //                   Introduction                  //
    /////////////////////////////////////////////////////
    // This code example shows how to compute the ring //
    // signatures from a stream of bytes.              //
    //                                                 //
    // A ring signature algorithm, only the signer and //
    // verifier recognise the signature.               //
    //                                                 //
    // For an external observer or a decoy should be   //
    // unfeasible to understand the signer or verifier // 
    // of the signature. Furthermore, it should be not //
    // feasible to forge the signature ( impersonate   //
    // the signer ).                                   //
    /////////////////////////////////////////////////////

    /////////////////////////////////////////////////////
    //                   Pre-requisits                 //
    /////////////////////////////////////////////////////
    // The Xoron matrix decompressed data structure    //
    // needs to be mined before running this code      //
    // example. Some examples on how to mine Xoron     //
    // matrices can be found in                        //
    // generate_xrn_matrix_all_rnd.c or                //
    // generate_xrn_matrix_one_third.c                 //
    //                                                 //
    // A Monomial key for the signer.                  //
    // A Binomial key for the signer.                  //
    // A Monomial key for the verifier.                //
    // A Binomial key for the verifier.                //
    // Binomial keys for the decoys.                   //
    //                                                 //
    // An example on how to generate keys can be found //
    // in generate_keys_with_wrapper_functions.c       //
    /////////////////////////////////////////////////////

int generate_keys_bytes(
 )
{

    FILE *start_point_fp;
    FILE *signer_monomial_key_fp;
    FILE *signer_binomial_key_fp;
    FILE *verifier_monomial_key_fp;
    FILE *verifier_binomial_key_fp;
    FILE *binomial_key_decoy_fp;
    FILE *decompressed_xrn_matrix_fp;

    // used to create decoys

    xbinomial_key_t decoy;
    xstart_point_t xstart;

    // library run time settings

    xrn_crypto_extra_settings_t settings;

    // all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    /////////////////////////////
    // Variable initialization //
    /////////////////////////////

    // Initialize default run time settings
    // In this code example only the settings.rnd_str is used
    // In the xrn_load_default_settings function xrn_make_start_point string is used.
    // Other functions names can be also used to initialize settings

    if ( XSUCCESS != xrn_load_default_settings( &settings ) ) {
        return -1;
    }

    /////////////////
    // Start point //
    /////////////////

    printf( "making start point ...\n" );

    start_point_fp = fopen( "start_point.xf", "wb" );
    decompressed_xrn_matrix_fp = fopen( "xrn_matrix.xm", "rb" );
    if ( ( start_point_fp == NULL ) || ( decompressed_xrn_matrix_fp == NULL ) ) {
        return -1;
    }

    if ( XSUCCESS != xrn_make_start_point_wrapper( start_point_fp, decompressed_xrn_matrix_fp, settings ) ) {
        fclose( start_point_fp );
        fclose( decompressed_xrn_matrix_fp );
        return -1;
    }

    if ( ( 0 != fclose( start_point_fp ) ) || ( 0 != fclose( decompressed_xrn_matrix_fp ) ) ) {
        return -1;
    }

    /////////////////////////
    // Monomial keys point //
    /////////////////////////

    printf( "making monomial keys ...\n" );

    signer_monomial_key_fp = fopen( "signer_monomial_key.xf", "wb" );
    if ( signer_monomial_key_fp == NULL ) {
        return -1;
    }

    if ( XSUCCESS != xrn_make_monomial_key_wrapper( signer_monomial_key_fp, settings ) ) {
        fclose( signer_monomial_key_fp );
        return -1;
    }

    if ( 0 != fclose( signer_monomial_key_fp ) ) {
        return -1;
    }

    verifier_monomial_key_fp = fopen( "verifier_monomial_key.xf", "wb" );
    if ( verifier_monomial_key_fp == NULL ) {
        return -1;
    }

    if ( XSUCCESS != xrn_make_monomial_key_wrapper( verifier_monomial_key_fp, settings ) ) {
        fclose( verifier_monomial_key_fp );
        return -1;
    }

    if ( 0 != fclose( verifier_monomial_key_fp ) ) {
        return -1;
    }

    ///////////////////////////
    // Binomial keys signer  //
    ///////////////////////////

    printf( "making signer binomial keys ...\n" );

    start_point_fp = fopen( "start_point.xf", "rb" );
    decompressed_xrn_matrix_fp = fopen( "xrn_matrix.xm", "rb" );
    signer_monomial_key_fp = fopen( "signer_monomial_key.xf", "rb" );
    signer_binomial_key_fp = fopen( "signer_binomial_key.xf", "wb" );

    if ( ( start_point_fp == NULL ) || ( decompressed_xrn_matrix_fp == NULL )
         || ( signer_monomial_key_fp == NULL ) || ( signer_binomial_key_fp == NULL ) ) {
        return -1;
    }

    if ( XSUCCESS !=
         xrn_make_binomial_key_wrapper( decompressed_xrn_matrix_fp, start_point_fp, signer_monomial_key_fp,
                                        signer_binomial_key_fp, settings ) ) {
        fclose( signer_binomial_key_fp );
        fclose( decompressed_xrn_matrix_fp );
        fclose( signer_monomial_key_fp );
        fclose( start_point_fp );
        return -1;
    }

    if ( ( 0 != fclose( signer_monomial_key_fp ) ) || ( 0 != fclose( signer_binomial_key_fp ) ) ) {
        return -1;
    }

    rewind( decompressed_xrn_matrix_fp );
    rewind( start_point_fp );

    /////////////////////////////
    // Binomial keys verifier  //
    /////////////////////////////

    printf( "making verifier binomial keys ...\n" );

    verifier_monomial_key_fp = fopen( "verifier_monomial_key.xf", "rb" );
    verifier_binomial_key_fp = fopen( "verifier_binomial_key.xf", "wb" );
    if ( ( verifier_binomial_key_fp == NULL ) || ( verifier_monomial_key_fp == NULL ) ) {
        fclose( decompressed_xrn_matrix_fp );
        fclose( start_point_fp );
        return -1;
    }

    if ( XSUCCESS !=
         xrn_make_binomial_key_wrapper( decompressed_xrn_matrix_fp, start_point_fp, verifier_monomial_key_fp,
                                        verifier_binomial_key_fp, settings ) ) {
        fclose( verifier_binomial_key_fp );
        fclose( decompressed_xrn_matrix_fp );
        fclose( verifier_monomial_key_fp );
        fclose( start_point_fp );
        return -1;
    }

    if ( ( 0 != fclose( verifier_monomial_key_fp ) ) || ( 0 != fclose( verifier_binomial_key_fp ) )
         || ( 0 != fclose( decompressed_xrn_matrix_fp ) ) ) {
        return -1;
    }

    ///////////////////////
    // Generating decoys //
    ///////////////////////

    printf( "making decoy 1 ...\n" );

    rewind( start_point_fp );

    if ( XSUCCESS != xrn_load_start_point( &xstart, start_point_fp, &settings ) ) {
        fclose( start_point_fp );
    }

    if ( 0 != fclose( start_point_fp ) ) {
        return 1;
    }

    decoy.start = xstart.start;

    // first decoy

    xrn_initialize_true_rnd_num( settings.rnd_str, &decoy.end );

    binomial_key_decoy_fp = fopen( "binomial_key_decoy_1.xf", "wb" );
    if ( binomial_key_decoy_fp == NULL ) {
        return 1;
    }

    if ( XSUCCESS != xrn_dump_binomial_key( decoy, binomial_key_decoy_fp, settings ) ) {
        fclose( binomial_key_decoy_fp );
        return 1;
    }

    if ( 0 != fclose( binomial_key_decoy_fp ) ) {
        return 1;
    }

    // second decoy

    printf( "making decoy 2 ...\n" );

    xrn_initialize_true_rnd_num( settings.rnd_str, &decoy.end );

    binomial_key_decoy_fp = fopen( "binomial_key_decoy_2.xf", "wb" );

    if ( binomial_key_decoy_fp == NULL ) {
        return 1;
    }

    if ( XSUCCESS != xrn_dump_binomial_key( decoy, binomial_key_decoy_fp, settings ) ) {
        fclose( binomial_key_decoy_fp );
        return 1;
    }

    if ( 0 != fclose( binomial_key_decoy_fp ) ) {
        return 1;
    }

    return 0;

}

int xrn_generate_ring_signature_bytes_wrapper_example(
 )
{

    /////////////////////////
    // Variable definition //
    /////////////////////////

    FILE *signer_monomial_key_fp;
    FILE *signer_binomial_key_fp;
    FILE *verifier_binomial_key_fp;
    FILE **binomial_key_decoys_fp;
    FILE *decompressed_xrn_matrix_fp;
    FILE *ring_signature_fp;

    // number of decoys
    uint16_t ndecoys;

    // library run time settings

    xrn_crypto_extra_settings_t settings;

    // all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    /////////////////////////////
    // Variable initialization //
    /////////////////////////////

    // Initialize default run time settings
    // In this code example only the settings.rnd_str is used
    // In the xrn_load_default_settings function xrn_make_start_point string is used.
    // Other functions names can be also used to initialize settings

    if ( XSUCCESS != xrn_load_default_settings( &settings ) ) {
        return -1;
    }

    ndecoys = 2;
    binomial_key_decoys_fp = ( FILE ** ) malloc( sizeof( FILE * ) * ndecoys );

    if ( binomial_key_decoys_fp == NULL ) {
        return -1;
    }

    signer_monomial_key_fp = fopen( "signer_monomial_key.xf", "rb" );
    signer_binomial_key_fp = fopen( "signer_binomial_key.xf", "rb" );
    verifier_binomial_key_fp = fopen( "verifier_binomial_key.xf", "rb" );
    decompressed_xrn_matrix_fp = fopen( "xrn_matrix.xm", "rb" );
    ring_signature_fp = fopen( "ring_signature.xf", "wb" );
    binomial_key_decoys_fp[0] = fopen( "binomial_key_decoy_1.xf", "rb" );
    binomial_key_decoys_fp[1] = fopen( "binomial_key_decoy_2.xf", "rb" );

    if ( ( binomial_key_decoys_fp[1] == NULL ) || ( binomial_key_decoys_fp[0] == NULL )
         || ( signer_monomial_key_fp == NULL ) || ( signer_binomial_key_fp == NULL )
         || ( verifier_binomial_key_fp == NULL ) || ( decompressed_xrn_matrix_fp == NULL )
         || ( ring_signature_fp == NULL ) ) {
        free( binomial_key_decoys_fp );
        return -1;
    }

    ////////////////////
    // Ring signature //
    ////////////////////

    if ( XSUCCESS != xrn_make_ring_signature_bytes_wrapper( ( uint8_t * ) LOREM_IPSUM_STR,      // input bytes
                                                            strlen( LOREM_IPSUM_STR ),  // length of the input bytes
                                                            binomial_key_decoys_fp,     // list of decoys file pointers
                                                            signer_monomial_key_fp,     // monomial key of the signer file pointer
                                                            signer_binomial_key_fp,     // binomial key of the signer file pointer
                                                            verifier_binomial_key_fp,   // binomial key of the verifier file pointer
                                                            decompressed_xrn_matrix_fp, // Xoron matrix file pointer
                                                            ndecoys,    // number of decoys (length of the file pointer array of binomial_key_decoys_fp)
                                                            ring_signature_fp,  // output ring signature file pointers
                                                            settings    // run time settings
          ) ) {
        fclose( ring_signature_fp );
        fclose( decompressed_xrn_matrix_fp );
        fclose( verifier_binomial_key_fp );
        fclose( signer_binomial_key_fp );
        fclose( signer_monomial_key_fp );
        fclose( binomial_key_decoys_fp[0] );
        fclose( binomial_key_decoys_fp[1] );
        free( binomial_key_decoys_fp );
        return -1;
    }

    if ( ( fclose( ring_signature_fp ) != 0 ) || ( fclose( decompressed_xrn_matrix_fp ) != 0 )
         || ( fclose( verifier_binomial_key_fp ) != 0 ) || ( fclose( signer_binomial_key_fp ) != 0 )
         || ( fclose( signer_monomial_key_fp ) != 0 ) || ( fclose( binomial_key_decoys_fp[0] ) != 0 )
         || ( fclose( binomial_key_decoys_fp[1] ) != 0 ) ) {
        free( binomial_key_decoys_fp );
        return -1;
    }

    // An alternative way to perform the same computation can be achieved using the xrn_make_ring_signature_bytes function
    // if ( XSUCCESS != xrn_make_ring_signature_bytes ( ( uint8_t * ) LOREM_IPSUM_STR,
    //                                          strlen(LOREM_IPSUM_STR),
    //                                          binomial_key_decoys, // binomial_key_decoys is of the type ( xbinomial_key_t * )
    //                                          signer_monomial_key, // signer_monomial_key is of the type ( xmonomial_key_t )
    //                                          signer_binomial_key, // signer_binomial_key is of the type ( xbinomial_key_t )
    //                                          verifier_binomial_key, // verifier_binomial_key is of the type ( xbinomial_key_t )
    //                                          xrn_matrix, // Xoron matrix is of the type ( xrn_matrix_t * )
    //                                          ndecoys,
    //                                          ring_signature , // ring_signature is of the type ( xring_signature_t * )
    //                                          settings
    //       ) ) {

    return 0;

}

int xrn_verify_ring_signature_bytes_wrapper_example(
 )
{

    /////////////////////////
    // Variable definition //
    /////////////////////////

    FILE *verifier_monomial_key_fp;
    FILE *signer_binomial_key_fp;
    FILE *decompressed_xrn_matrix_fp;
    FILE *ring_signature_fp;

    // library run time settings
    xrn_crypto_extra_settings_t settings;

    // all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

    /////////////////////////////
    // Variable initialization //
    /////////////////////////////

    // Initialize default run time settings
    // In this code example only the settings.rnd_str is used
    // In the xrn_load_default_settings function xrn_make_start_point string is used.
    // Other functions names can be also used to initialize settings

    if ( XSUCCESS != xrn_load_default_settings( &settings ) ) {
        return -1;
    }

    verifier_monomial_key_fp = fopen( "verifier_monomial_key.xf", "rb" );
    signer_binomial_key_fp = fopen( "signer_binomial_key_from_verifier.xf", "wb" );
    decompressed_xrn_matrix_fp = fopen( "xrn_matrix.xm", "rb" );
    ring_signature_fp = fopen( "ring_signature.xf", "rb" );

    if ( ( verifier_monomial_key_fp == NULL ) || ( signer_binomial_key_fp == NULL )
         || ( decompressed_xrn_matrix_fp == NULL ) || ( ring_signature_fp == NULL ) ) {
        return -1;
    }

    ////////////////////
    // Ring signature //
    ////////////////////

    if ( XSUCCESS != xrn_check_ring_signature_bytes_wrapper( ( uint8_t * ) LOREM_IPSUM_STR,     // input bytes
                                                             strlen( LOREM_IPSUM_STR ), // length of the input bytes
                                                             ring_signature_fp, // ring signature file pointers
                                                             verifier_monomial_key_fp,  // monomial key of the verifier file pointer
                                                             decompressed_xrn_matrix_fp,        // Xoron matrix file pointer
                                                             signer_binomial_key_fp,    // binomial key of the signer file pointer
                                                             settings   // run time settings
          ) ) {
        fclose( ring_signature_fp );
        fclose( decompressed_xrn_matrix_fp );
        fclose( signer_binomial_key_fp );
        fclose( verifier_monomial_key_fp );
        return -1;
    }

    if ( ( fclose( ring_signature_fp ) != 0 ) || ( fclose( decompressed_xrn_matrix_fp ) != 0 )
         || ( fclose( signer_binomial_key_fp ) != 0 ) || ( fclose( verifier_monomial_key_fp ) != 0 ) ) {
        return -1;
    }

    // An alternative way to perform the same computation can be achieved using the xrn_check_ring_signature_bytes function
    // if ( XSUCCESS != xrn_check_ring_signature_bytes ( ( uint8_t * ) LOREM_IPSUM_STR,
    //                                          strlen(LOREM_IPSUM_STR),
    //                                          ring_signature, // ring_signature is of the type ( xring_signature_t )
    //                                          verifier_monomial_key, // verifier_monomial_key is of the type ( xmonomial_key_t )
    //                                          xrn_matrix, // Xoron matrix is of the type ( xrn_matrix_t * )
    //                                          signer_binomial_key, // signer_binomial_key is of the type ( xbinomial_key_t * )
    //                                          settings
    //       ) ) {

    return 0;

}

int ring_signature_bytes_wrapper(
 )
{

    /////////////
    // Logging //
    /////////////

    // Set the default logging file streams
    // errors -> stderr
    // warnings -> stdout
    // notifications -> stdout
    // debug -> stdout

    xrn_set_default_log(  );

    // To change the default logging you can use the functions below,
    // where fp is the opened file pointer
    // 
    // xrn_change_error_log( fp )
    // xrn_change_warning_log( fp )
    // xrn_change_notification_log( fp )
    // xrn_change_debug_log( fp )

    if ( 0 != generate_keys_bytes(  ) ) {
        return -1;
    }

    printf( "making ring signature ... \n" );
    if ( 0 != xrn_generate_ring_signature_bytes_wrapper_example(  ) ) {
        printf( "error in signature \n" );
        return -1;
    }

    printf( "verify ring signature ... \n" );
    if ( 0 != xrn_verify_ring_signature_bytes_wrapper_example(  ) ) {
        printf( "signature does not match\n" );
        return -1;
    } else {
        printf( "signature does match\n" );
        return 0;
    }

    return 0;

}

